package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author shaki
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {
    @Resource
    private UcenterMemberService memberService;

    /**
     * 登录
     * requestbody必须用post提交
     */
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member){
        //token use jwt 生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    /**
     * 根据用户id获取用户信息，供远程调用
     */
    @PostMapping("getUserInfoOder/{memberId}")
    public UcenterMemberOrder getUserInfoOder(@PathVariable("memberId") String memberId){
        UcenterMember member = memberService.getById(memberId);
        //把member对象复制给ucentermemberorder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /**
     * 查询某一天注册人数
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("count",count);
    }
}

